package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Customers;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.MyPopupWindowListener;

import java.util.List;


/**
 * Created by hacker on 2017/7/2.
 */

public class SelectCustomerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Customers> mCustomerList;
    private Context mContent;
    private MyPopupWindowListener myPopupWindowListener;

    public SelectCustomerAdapter(List<Customers> list, Context content, MyPopupWindowListener listener) {
        this.mCustomerList = list;
        this.mContent = content;
        this.myPopupWindowListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (mCustomerList.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.bindViewData(mCustomerList.get(position));
            viewHolder.setViewListener(mCustomerList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (mCustomerList.size() > 0) {
            return mCustomerList.size();
        } else {
            return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {


        TextView plateNumberTV;


        public ViewHolder(View itemView) {
            super(itemView);
            plateNumberTV = (TextView) itemView.findViewById(R.id.plate_number);

        }

        public void setViewListener(final Customers customer) {
            plateNumberTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myPopupWindowListener.OnMyClick(Constant.SELECT_TYPE_CUSTOMER,
                            Integer.valueOf(customer.getId() + ""),
                            customer.getCompanyname());
                }
            });
        }

        public void bindViewData(final Customers customer) {
            plateNumberTV.setText(customer.getCompanyname());
        }
    }
}
